package com.fans.chapter5.four;

import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

public class Task  extends RecursiveTask<Integer>{
	private int array [];
	private int start , end;
	
	public Task(int[] array, int start, int end) {
		super();
		this.array = array;
		this.start = start;
		this.end = end;
	}

	@Override
	protected Integer compute() {
		System.out.printf("Task : Start from %d to %d\n",start,end);
		if(end - start<10){
			if((3>start) && (3<end)){
				throw new RuntimeException("This task throws an Exception : Task from "+start+"to "+end);
			}
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}else{
			int mid = (end+start) / 2;
			Task task = new Task(array,start,mid);
			Task task2 = new Task(array,mid,end);
			invokeAll(task,task2);
		}
		System.out.printf("Task : End form %d to %d\n",start,end);
		return 0;
	}
}
